<template>
  <el-dialog width="400" :title="dialogTitle" v-model="dialogVisible" modal @close="closeDialog" draggable
    :close-on-click-modal="false" :append-to-body="true">
    <el-form class="dialog-form" :model="formData" ref="vForm" label-position="right" label-width="80px" size="medium"
      @submit.native.prevent :rules="formRules">
      <el-form-item label="名称" prop="itemText">
        <el-input v-model="formData.itemText" placeholder="请输入名称" link clearable />
      </el-form-item>
      <el-form-item label="数据值" prop="itemValue">
        <el-input v-model="formData.itemValue" placeholder="请输入数据值" link clearable />
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input v-model="formData.description" placeholder="请输入描述" type="textarea" :rows="2" />
      </el-form-item>
      <el-form-item label="排序" prop="sortOrder">
        <el-input-number v-model="formData.sortOrder" :min="1" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog" size="mini">取 消</el-button>
      <el-button type="primary" @click="submitForm" size="mini">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addItem, updateItem, getItemInfo } from '@/api/sys/dict'
export default {
  name: 'dictItemDialog',
  data() {
    return {
      type: '',
      dialogTitle: '',
      dialogVisible: false,
      formData: {},
      formDisabled: false,
      formRules: {
        itemText: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        itemValue: [{ required: true, message: '请输入数据值', trigger: 'blur' }],
      },
    }
  },
  created() { },
  methods: {
    add(dictId) {
      this.formDisabled = false
      this.type = 'add'
      this.dialogTitle = '新增字典项'
      // 先清空表单数据
      this.formData = {
        dictId: dictId,
        sortOrder: 1,
      }
      this.dialogVisible = true
    },
    edit(row) {
      this.formDisabled = true
      this.type = 'edit'
      this.dialogTitle = '新增字典项'
      getItemInfo(row.dictItemId)
        .then((res) => {
          if (res.data.code === 200) {
            this.formData = res.data.data
            this.dialogVisible = true
          } else {
            this.$message.error(res.data.message)
          }
        })
        .catch((err) => {
          this.$message.error(err)
        })
    },
    submitForm() {
      this.$refs['vForm'].validate((valid) => {
        if (valid) {
          if (this.type === 'add') {
            addItem(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('新增成功')
                  this.closeDialog()
                  this.$emit('ok')
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          } else {
            updateItem(this.formData)
              .then((res) => {
                if (res.data.code === 200) {
                  this.$message.success('编辑成功')
                  this.closeDialog()
                  this.$emit('ok')
                } else {
                  this.$message.warning(res.data.message)
                }
              })
              .catch((err) => {
                this.$message.error(err)
              })
          }
        }
      })
    },
    closeDialog() {
      this.$refs['vForm'].resetFields()
      this.dialogVisible = false
    },
  },
}
</script>

<style scoped>
.dialog-form {
  padding: 0 30px;
  /* 根据需求调整数值 */
}
</style>
